﻿''' <summary>
''' 全局报警
''' </summary>
''' <remarks></remarks>
Public Class DeviceGlobal

    ''' <summary>
    ''' 设备列表
    ''' </summary>
    ''' <remarks></remarks>
    Private Devs As Hashtable

    ''' <summary>
    ''' 设备的顶点坐标
    ''' </summary>
    ''' <remarks></remarks>
    Private iTop As Integer = 0
 
    '''' <summary>
    '''' 设备编号
    '''' </summary>
    '''' <remarks></remarks>
    ' Private iDeviceId As Integer = 0

    Private dxt As Integer = 1         ' 界面排列时上下间隔
    Private dyt As Integer = 19        ' 界面排列时左右间隔
    Private dcolumn As Integer = 2     ' 界面显示的列数

    Private projectId As String = 0    ' 项目编号

      ''' <summary>
    ''' 使用并联机组编号进行初始化
    ''' </summary>
    ''' <param name="pId">项目编号</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal pId As String)

        InitializeComponent()
        Me.projectId = pId
        Init()
    End Sub


    ''' <summary>
    ''' 初始化机组端口
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Init()
        Devs = New Hashtable

        Dim ds As DataSet = Nothing
        Dim dal As CcapSetPortService = New CcapSetPortService
        '' A.Id,A.DevId,B.csNetId,A.CpsLostPowerAlarmPort, A.CpsBreakDownAlarmPort
        ds = dal.GetPorts(Me.projectId)

        If (ds IsNot Nothing) AndAlso ds.Tables(0).Rows.Count > 0 Then


            For index As Integer = 0 To ds.Tables(0).Rows.Count - 1
                Dim dr As DataRow = ds.Tables(0).Rows(index)
                CreatePorts(CInt(dr(3)), CInt(dr(1)), CInt(dr(2)), "系统断电", DeviceStatusTypes.LostPower)
                CreatePorts(CInt(dr(4)), CInt(dr(1)), CInt(dr(2)), "发电机启动故障保护", DeviceStatusTypes.StartFailure)
            Next
        End If

        ''===================
        '' 重新设置界面大小
        ReSizeControl()
    End Sub

    ''' <summary>
    ''' 创建端口，并添加到界面
    ''' </summary>
    ''' <param name="portId">设备端口号</param>
    ''' <param name="devId">机组编号</param>
    ''' <param name="portName ">端口名称</param>
    ''' <remarks></remarks>
    Private Sub CreatePorts(ByVal portId As Integer, ByVal devId As Integer, ByVal netId As Integer, ByVal portName As String, ByVal portType As DeviceStatusTypes)
        If (portId = 0) Then Exit Sub

        Dim ps As PortStatus
        ps = New PortStatus(portId, devId, portType)

        Dim iRow As Integer = 0
        Dim iCol As Integer = 0
        Dim iCount As Integer = Devs.Count

        iRow = Math.Floor(iCount / Me.dcolumn)
        iCol = iCount Mod Me.dcolumn

        ps.Location = New Point(iCol * (Me.dxt + ps.Width), iRow * Me.dyt)


        ps.PortName = portName

        ps.BorderStyle = Windows.Forms.BorderStyle.FixedSingle

        Me.pnlDevs.Controls.Add(ps)

        Try
            Devs.Add(netId * 100 + portId, ps)
        Catch ex As Exception

        End Try


    End Sub

    ''' <summary>
    ''' 获取端口号
    ''' </summary>
    ''' <remarks></remarks>
    Public Function GetPorts() As Hashtable
        Return Me.Devs
    End Function
    ''' <summary>
    ''' 从新计算控件的高度
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ReSizeControl()
        If Devs.Count > 0 Then
            Me.Height = (Math.Ceiling(Devs.Count / Me.dcolumn)) * Me.dyt + 20
        Else
            Me.Height = 20 + 1 + Me.lblGroupName.Height
        End If

        Me.lblGroupName.Top = (Me.pnlName.Height - Me.lblGroupName.Height) / 2
    End Sub
End Class
